<template>
  <view class="content pt-30 pb-50 pl-20 pr-20">
    <view class="detail-block pd-20">
      <view style="display: flex;">
        <view class="title-block">核查信息</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患名称</view>
        <view class="item-value">{{detail.htName ? detail.htName : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患等级</view>
        <view class="item-value">{{detail.levelName ? detail.levelName : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患描述</view>
        <view class="item-value">{{detail.description ? detail.description : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查单位</view>
        <view class="item-value">{{detail.checkDept ? detail.checkDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查人</view>
        <view class="item-value">{{detail.checkPeo ? detail.checkPeo : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查类型</view>
        <view class="item-value">{{detail.checkType ? checkTypeJson[detail.checkType] : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患地点</view>
        <view class="item-value">{{detail.place ? detail.place : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">所属风险点</view>
        <view class="item-value">{{detail.riskPoint ? detail.riskPoint : ''}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">所属单位</view>
        <view class="item-value">{{detail.beCheckDept ? detail.beCheckDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改方式</view>
        <view class="item-value">{{detail.rectificationType == 1 ? '限期整改' : '立即整改'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改责任单位</view>
        <view class="item-value">{{detail.rectificationDept ? detail.rectificationDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改责任人</view>
        <view class="item-value">{{detail.rectificationPeo ? detail.rectificationPeo : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改期限</view>
        <view class="item-value">{{detail.rectificationLimit ? detail.rectificationLimit : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">剩余时间</view>
        <view class="item-value">{{getRemainingTime(nowDateTime, detail.rectificationLimit)}}</view>
      </view>
      <view class="item-block pt-20 border-b-common">
        <view class="item-label">整改前图片</view>
        <view class="image-list">
          <image v-for="(item, index) in detail.ossImg" :key="index" class="image" mode="aspectFill" :src="item.url" @click="previewImage(item.url)"></image>
        </view>
      </view>
      <view class="item-block pt-20 pb-20">
        <view class="item-label">延期日期</view>
        <view class="item-value">{{detail.deferDate ? detail.deferDate : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20">
        <view class="item-label">延期原因</view>
        <view class="item-value">{{detail.deferApplyInfo ? detail.deferApplyInfo : '-'}}</view>
      </view>
    </view>
    <view v-if="cancelState" class="detail-block pd-20 mt-30">
      <view class="mb-20" style="display: flex;">
        <view class="title-block">驳回原因</view>
      </view>
      <view>
        <textarea class="text-area pd-20" placeholder="请输入驳回原因" placeholder-class="placeholder" v-model="params.deferApproveInfo"></textarea>
      </view>
    </view>
    <view v-if="!cancelState" class="button-block mt-30">
      <u-button type="error" text="驳回" :custom-style="{width: '340rpx'}" @click="changeCancelState(true)"></u-button>
      <u-button type="primary" text="同意" :custom-style="{width: '340rpx'}" @click="submit('1')"></u-button>
    </view>
    <view v-else class="button-block mt-30">
      <u-button type="info" text="取消" :custom-style="{width: '340rpx', backgroundColor: '#FFFFFF !important'}" @click="changeCancelState(false)"></u-button>
      <u-button type="error" text="确认" :custom-style="{width: '340rpx'}" @click="submit('0')"></u-button>
    </view>
  </view>
</template>

<script>
  import { getEditDelayDetailApi, updateEditDelayInfoApi } from '@/apis/danger/index.js'
  import checkTypeJson from '@/static/json/checkType.json'
  import dayjs from 'dayjs'
  
  export default {
    data() {
      return {
        checkTypeJson,
        cancelState: false,
        params: {},
        detail: {
          deferApproveResult: '',
          deferApproveInfo: ''
        },
        nowDateTime: dayjs().valueOf()
      }
    },
    onLoad(options) {
      if(options.id) this.getEditDelayDetail({id: options.id})
    },
    methods: {
      getEditDelayDetail: function(params) {
        getEditDelayDetailApi(params).then(res => {
          this.detail = res
        }).catch(err => console.warn(err))
      },
      submit: function(type) {
        let params = this.params
        if(type == '0' && !params.deferApproveInfo) {
          uni.showToast({title: '请输入驳回原因', icon: 'none'})
          return false
        }
        Object.assign(params, {deferApproveResult: type == '1' ? '同意': '拒绝', id: this.detail.id, htId: this.detail.htId})
        updateEditDelayInfoApi(params).then(res => {
          setTimeout(() => uni.navigateBack(), 500)
        }).catch(err => console.warn(err))
      },
      previewImage: function(url) {
        uni.previewImage({ urls: [url] })
      },
      getRemainingTime: function(startTime = dayjs().format('YYYY-MM-DD HH:mm:ss'), endTime = dayjs().format('YYYY-MM-DD HH:mm:ss')) {
        if(dayjs(startTime).valueOf() < dayjs(endTime).valueOf()){
          var dayaa = dayjs(endTime).valueOf() - dayjs(startTime).valueOf()
          var days = parseInt(dayaa / (1000 * 60 * 60 * 24));
          var hours = parseInt((dayaa % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          return days + " 天 " + hours + " 小时 " ;
        }else{
          var dayaa = dayjs(startTime).valueOf() -  dayjs(endTime).valueOf()
          var days = parseInt(dayaa / (1000 * 60 * 60 * 24));
          var hours = parseInt((dayaa % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          return "已逾期 "+ days + " 天 " + hours + " 小时 " ;
        }
        // return startTime && endTime && (dayjs(startTime).valueOf() < dayjs(endTime).valueOf()) ? dayjs(endTime).diff(startTime, 'hour') : 0
      },
      changeCancelState: function(state) {
        this.params.deferApproveInfo = ''
        this.cancelState = state
      }
    }
  }
</script>

<style>
  page {
    background-color: #F4F4F4;
  }
</style>

<style lang="scss" scoped>
  .content {
    .detail-block {
      border-radius: 6rpx;
      background-color: #FFFFFF;
      .title-block {
        font-size: 30rpx;
        line-height: 42rpx;
        color: #222222;
        font-weight: 500;
        background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 50%, #C5DBFF 50%, #C5DBFF);
      }
      .item-block {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .item-label {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #333333;
        }
        .item-value {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #999999;
          max-width: 500rpx;
        }
        .image-list {
          max-width: 520rpx;
          display: flex;
          justify-content: flex-end;
          flex-direction: row-reverse;
          flex-wrap: wrap;
          .image {
            width: 156rpx;
            height: 120rpx;
            border-radius: 6rpx;
            background-color: #CCCCCC;
            margin-left: 20rpx;
            margin-bottom: 20rpx;
          }
          .image:nth-child(3n) {
            margin-left: 0;
          }
        }
      }
      .text-area {
        width: 630rpx; 
        height: 300rpx;
        font-size: 28rpx;
        line-height: 40rpx;
        color: #333333;
        border: 1rpx solid #EEEEEE;
        border-radius: 6rpx;
      }
      .placeholder {
        font-size: 28rpx;
        line-height: 40rpx;
        color: #999999;
      }
    }
    .button-block {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
</style>
